package com.dxy.df.modules.scheduler;

import com.dxy.df.constants.CopyWriterConst;
import com.dxy.df.modules.bean.put.DfArticlePut;
import com.dxy.df.modules.bean.put.DfCommentPut;
import com.dxy.df.modules.bean.vo.DfArticleVo;
import com.dxy.df.modules.service.DfArticleService;
import com.dxy.df.modules.service.DfCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author: XiaoFeng Cui (cuixiaofeng@dxy.cn)
 * @date : 2019/10/25
 * @description:
 */

@Component
public class RebotScheduled {


    @Autowired
    private DfArticleService dfArticleService;

    @Autowired
    private DfCommentService dfCommentService;

    @Transactional
    @Scheduled(cron = "0 * * * * *")
    public void autoComment(){

        //获取一个小时未被评论的内容
        List<DfArticleVo> articles = dfArticleService.selectNoCommentArticle();

        //内容分析，进行逗比评论
        articles.forEach(article ->{
            try {
                DfCommentPut put = new DfCommentPut();
                put.setArticleId(article.getId());
                String content = article.getContent();
                if (content.length() > 500) {
                    //随机获取逗比文案进行评论
                    put.setContent(CopyWriterConst.LEN_500);
                } else if (content.length() > 200) {
                    //随机获取逗比文案进行评论
                    put.setContent(CopyWriterConst.LEN_200);
                } else {
                    put.setContent(CopyWriterConst.LEN_100);
                }
                put.setCreateBy("robot");
                put.setCreateTime(new Date());
                put.setUpdateBy("robot");
                put.setUpdateTime(new Date());
                dfCommentService.save(put);
            }catch (Exception e){
                e.printStackTrace();
            }
        });

    }




}
